package swu.edu.cn.javaee.service;

import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import swu.edu.cn.javaee.entity.Topic;
import swu.edu.cn.javaee.mapper.TopicMapper;
import static swu.edu.cn.javaee.component.Signal.*;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Service
public class TopicService {
    @Resource
    private TopicMapper topicMapper;


    //新建帖子
    public String saveTopic(String title,String content,String author){
        Date publish_time=new Date();
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Topic topic=new Topic();
        topic.setTitle(title);
        topic.setContent(content);
        topic.setAuthor(author);
        topic.setPublish_time(format.format(publish_time));
        topicMapper.saveTopic(topic);
        System.out.println(topic);
        return SUCCESS_CODE;
    }

    //根据标题查找帖子
    public List<Topic> queryTopicByTitle(String title){
        List<Topic> topicList=topicMapper.queryTopicByTitle(title);
        System.out.println(topicList);
        return topicList;
    }

    //删除帖子
    public String deleteTopic(String tid){
        Topic result=topicMapper.queryTopicById(tid);
        if (result!=null){
            topicMapper.deleteTopic(result);
            return SUCCESS_CODE;
        }
        else {
            return FAILED_CODE_NO_TOPIC;
        }
    }
}
